package com.wbao.dianniu.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.listener.IAccountModifyListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AccountModifyManager;
import com.wbao.dianniu.R;

/**
 * 隐私设置
 * 
 * @author yejianjun
 * 
 */
public class PrivacySettingActivity extends BaseActivity implements IAccountModifyListener{

	private RelativeLayout rl_check, rl_add;
	Dialog dialog;
	private AccountModifyManager manager;
	private String[] dataPrivacy = new String[]{"所有人可见","好友可见"};
	private String[] beFriend = new String[]{"所有人","不允许加为好友"};
	private TextView dataprivacyTV;
	private TextView beFriendTV;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_privacy_setting);
		setTitleName("隐私设置");
		rl_check = (RelativeLayout) findViewById(R.id.rl_privacy_check_info);
		rl_add = (RelativeLayout) findViewById(R.id.rl_privacy_add_friend);
		dataprivacyTV = (TextView) findViewById(R.id.privacy_dataprivacy_tv);
		beFriendTV = (TextView) findViewById(R.id.privacy_befriend_tv);
		rl_check.setOnClickListener(this);
		rl_add.setOnClickListener(this);
		manager = new AccountModifyManager(this);
		manager.addAccountModifyListener(this);
		if( 0 == GlobalContext.dataprivacy){
			dataprivacyTV.setText(dataPrivacy[0]);
		}else {
			dataprivacyTV.setText(dataPrivacy[1]);
		}
		
		if( 0 == GlobalContext.beFriend){
			beFriendTV.setText(beFriend[0]);
		}else {
			beFriendTV.setText(beFriend[1]);
		}
		
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.rl_privacy_check_info:
			ShowDialog(1,dataPrivacy[0],dataPrivacy[1]);
			break;
		case R.id.rl_privacy_add_friend:
			ShowDialog(2,beFriend[0],beFriend[1]);
			break;
		default:
			break;
		}

	}

	@SuppressLint("NewApi")
	private void ShowDialog(final int type,String text1,String text2) {
		dialog = new Dialog(this, R.style.custom_Dialog); 
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(10,10, 10, 10);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
		View view = LayoutInflater.from(this).inflate(R.layout.privacy_setting_dialog, null);
	    dialog.setContentView(view); 
	    Button oneButton = (Button) view.findViewById(R.id.privacy_all_button);
	    Button twoButton = (Button) view.findViewById(R.id.privacy_none_button);
	    oneButton.setText(text1);
	    twoButton.setText(text2);
	    oneButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				if(1 == type){
//					manager.modify(GlobalContext.accountId, "dataPrivacy", "0");
					dataprivacyTV.setText(dataPrivacy[0]);
					GlobalContext.dataprivacy = 0;
				}else if(2 == type){
					beFriendTV.setText(beFriend[0]);
//					manager.modify(GlobalContext.accountId, "beFriend", "0");
					GlobalContext.beFriend = 0;
				}
			}
		});
	    twoButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(null != dialog && dialog.isShowing()){
					dialog.dismiss();
				}
				if(1 == type){
//					manager.modify(GlobalContext.accountId, "dataPrivacy", "1");
					dataprivacyTV.setText(dataPrivacy[1]);
					GlobalContext.dataprivacy = 1; 
				}else if(2 == type){
					beFriendTV.setText(beFriend[1]);
//					manager.modify(GlobalContext.accountId, "beFriend", "1");
					GlobalContext.beFriend = 1;
				}
			}
		});
	    dialog.setCancelable(true); 
	    dialog.show(); 
	}

	@Override
	public void onModifyFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onModifySuccess() {
		// TODO Auto-generated method stub
		
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
